# _*_coding:utf-8_*_
"""
   ATS 1.0(Automated Test Server, Version 1.0)Global Data File parser,
   Developed by Ivy.Li 2013.03.18.
   (C)Copyright 2013 Hech. All rights reserved
"""
import configparser as ConfigParser
import os

# error messages define
INVAL_HEADER = 1003
INVAL_GLOBALDATA = 1004
NO_GDF_FOUND = 1009

'''
GDF类
'''


class GDF:
    """
    header: {}
    data:{
        'name': val
    }
    """

    def __init__(self, gdf=None):
        if gdf:
            self._load(gdf)
        else:
            self.clear()

    def clear(self):
        self.header = {}
        self.data = {}
        self.error = 0

    def _load(self, gdf):
        if not os.path.exists(gdf):
            self.error = NO_GDF_FOUND
            return
        self.clear()
        cp = ConfigParser.ConfigParser()
        cp.read(gdf)
        try:
            for k, v in cp.items('Header'):
                self.header[k] = v
        except:
            self.error = INVAL_HEADER
            return
        try:
            for k, v in cp.items('Gobal Data'):
                type = v[1:v.index(',')]
                if type == 'INT' or type == 'STRING' or type == 'CHAR':
                    tmp = v.split(',')
                    type, val = tmp[0][1:], tmp[1][:-1]
                    if type == 'INT':
                        self.data[k] = int(val)
                    elif type == 'STRING' or type == 'CHAR':
                        self.data[k] = str(val)
                elif type == 'FLOAT_ARRAY' or type == 'INT_ARRAY' or type == 'DOUBLE_ARRAY':
                    val = v[(v.index(',') + 1):-1]
                    self.data[k] = eval(val)

        except:
            self.error = INVAL_GLOBALDATA
            return

###Test code
# gdf=GDF(r'H:\MrBig_2013_0311\InputFileFormat\InputFileFormat Revised0318\a.gdf')
# print gdf.header
# print gdf.error,'===========error'
# print gdf.data
